#!/usr/bin/env python
# -*- coding: utf-8 -*-

# publishe mymessage
# BEGIN IMPORT
import rospy
# END IMPORT
 
# BEGIN STD_MSGS
from std_msgs.msg import Int32
from std_msgs.msg import String

order=-1

# END STD_MSGS
def talker():
    global order
    pub = rospy.Publisher('xfwakeup', String, queue_size=10) #发布消息到话题xfwakeup中,队列长度10
    pub1 = rospy.Publisher('listen', Int32, queue_size=10) #发布消息到话题xfwakeup中,队列长度10
    pub2 = rospy.Publisher('xfwords', String, queue_size=10) #发布消息到话题xfwakeup中,队列长度10
    rospy.init_node('talker', anonymous=True) #初始化节点名字为talker,加入一个随机数使得节点名称唯一
    rospy.Subscriber("xfspeech", String, InfoCallback)

    # rospy.spin()
    rate = rospy.Rate(0.14) # 0.14Hz=7s  设置发布频率
    while not rospy.is_shutdown(): #当没有异常关闭时候执行如下程序(防止ctrl+c 终止程序)
        if order==-1:
            hello_str = "ok"
            rospy.loginfo(hello_str) #写入log日志
            pub.publish(hello_str) #发布字符串
        else:
            pub1.publish(order)
            if order==0:
                hello_str = "I get it,I will come back.我知道了,我马上回来"
            elif order==1:
                hello_str = "I get it,I will go out.我知道了,我马上出去"
            elif order==2:
                hello_str = "I get it,I will take out the trash.我知道了,我马上去扔"
            rospy.loginfo(hello_str)
            pub2.publish(hello_str)
            rospy.sleep(25)
            order=-1

        rate.sleep() #配合发布频率的休眠
def InfoCallback(msg):
    global order
    if "Come back" in msg.data:
        order=0
    elif "Go out" in msg.data:
        order=1
    elif "Take out the trash" in msg.data:
        order=2
    #rospy.spin()

if __name__ == '__main__':
    try:
        talker()
    except rospy.ROSInterruptException:
        pass
